/**
 * Created by XianTao on 2016/11/7
 * <p/>
 * Copyright (c) 2015-2015
 * Apache License
 * Version 2.0, January 2004
 * http://www.apache.org/licenses/
 */
package org.lego.biz.wechat.lechuan.admin.api.restful;

import org.lego.biz.wechat.lechuan.base.entity.Prize;
import org.lego.biz.wechat.lechuan.base.service.PrizeService;
import org.lego.jpa.base.util.DynamicSpecifications;
import org.lego.jpa.base.util.ServletUtil;
import org.lego.jpa.base.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletRequest;

@RestController
@RequestMapping(value = "/admin/prizes")
public class PrizeAdminRestful {
    @Autowired
    private PrizeService prizeService;

    @RequestMapping(method = RequestMethod.GET)
    public ResultVo<Page<Prize>> list(@RequestParam(value = "page", defaultValue = "1") int pageNumber,
                                      @RequestParam(value = "rows", defaultValue = "10") int pageSize,
                                      ServletRequest request) {
        PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, ServletUtil.getParametersStartingWithSort(request));
        Specification<Prize> specification = DynamicSpecifications.buildSpecification(
                ServletUtil.getParametersStartingWithSearch(request), Prize.class);
        return new ResultVo<Page<Prize>>(prizeService.findAll(specification, pageRequest));
    }
}
